﻿using System;
using System.Data;
using System.Web.UI;
using KPIS.GERP.ESS.BLL;
using Telerik.Web.UI;
using System.Collections.Generic;
using KPIS.GERP.ESS.MODEL;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class ESS_ChooseApprovalLine : Page
    {
        private SsMng ssMng = new SsMng();
        private ComboESSBLL comboEssbll = new ComboESSBLL();
        private B_MessageSend send = new B_MessageSend();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadInitialData();
                ValidateDataLoaded();
                LoadApprovalPathRadTreeList();
                LoadApprovalLineRadTreeList();
            } else
            {
                ValidateDataLoaded();
            }
        }

        #region Load Data
        /// <summary>
        /// Message History ประวัติการส่งข้อความของโครงการ
        /// </summary>
        private void LoadApprovalPathRadTreeList()
        {
            M_MessageSend info = send.GetInfo(SendSeq);
            IList<M_MessageSend> infoList = send.GetApprovePath(info.ref_type, info.ref_seq.ToString());

            rtlRadApprovalPath.DataSource = infoList;
            rtlRadApprovalPath.DataBind();

            Page.Title = info.send_text;
            lblSender.Text = infoList[0].from_user_name;
        }
        
        /// <summary>
        /// Approval Line สายบังคับบัญชา
        /// </summary>
        protected void LoadApprovalLineRadTreeList()
        {
            DataTable dt = comboEssbll.LoadPositionWorkingJobsites(null, "");

            try
            {
                rtlRadApprovalLine.DataSource = dt;
                rtlRadApprovalLine.DataBind();

                if (Session["NodeSelectedValue"] != null)
                {
                    TreeListDataItem selectedNode =
                            rtlRadApprovalLine.Items.Find(o => o.ID == Session["NodeSelectedValue"].ToString());

                    selectedNode.Expanded = true;
                    selectedNode.Selected = true;
                    selectedNode.Focus();
                }
            }
            catch (Exception error)
            {
                throw error;
            }
            SetStyleTreeList();
        }

        private void LoadInitialData()
        {
            Mode = Request["mode"];
            ApprovePathSeq = Request["approvePathSeq"];
            NewApprovePathSeq = Request["newApprovePathSeq"];
            SendSeq = Request["sendSeq"];

        }

        private void ValidateDataLoaded()
        {
            if(String.IsNullOrEmpty(SendSeq))
            {
                throw new Exception("Please Specify Send Message Sequence.");
            }
        }
        #endregion

        #region Misc

        private void SetStyleTreeList()
        {
            int i = 0;
            string err = "";
            foreach (TreeListDataItem selectedNode in rtlRadApprovalLine.Items)
            {
                if (true)
                    // selectedNode.CssClass = PersonGeneralItemStyle;

                    if (ssMng.EmpSeq == selectedNode.Cells[0].Text)
                    {
                        selectedNode.CssClass = PersonYourselfItemStyle;
                        selectedNode.Expanded = true;
                        selectedNode.Selected = true;
                        selectedNode.Focus();
                    }
            }
        }
        #endregion

        #region Event
        protected void rtlRadApprovalPath_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" 
                || e.CommandName == "Page" || e.CommandName == "ExpandCollapse")
            {
                LoadApprovalPathRadTreeList();
            } 
        }

        protected void rtlRadApprovalLine_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" 
                || e.CommandName == "Page" || e.CommandName == "ExpandCollapse")
            {
                LoadApprovalLineRadTreeList();
            } 
        }
        #endregion

        #region Properties

        private string PersonGeneralItemStyle { get { return "RoundPersonalItem BackgroundPersonalItemBlue"; } }
        private string PersonInLineItemStyle { get { return "RoundPersonalItem BackgroundPersonalItemPink"; } }
        private string PersonYourselfItemStyle { get { return "RoundPersonalItem BackgroundPersonalItemYellow"; } }

        private string Mode
        {
            get { return hdfMode.Value; }
            set { hdfMode.Value = value; }
        }
        private string ApprovePathSeq
        {
            get { return hdfApprovePathSeq.Value; }
            set { hdfApprovePathSeq.Value = value; }
        }
        private string NewApprovePathSeq
        {
            get { return hdfNewApprovePathSeq.Value; }
            set { hdfNewApprovePathSeq.Value = value; }
        }
        private string SendSeq
        {
            get { return hdfSendSeq.Value; }
            set { hdfSendSeq.Value = value; }
        }

        #endregion  
    }
}